﻿using UnityEngine;
using System.Collections;
using System;



public class VungleAds:BaseAds<VungleAds>
{
	public static bool showBanner = false;
	static Action finishtodo = null;
	
	
	
	public VungleAds ()
	{
		Vungle.onAdStartedEvent += onAdStartedEvent;
		Vungle.onAdFinishedEvent += onAdFinishedEvent;
		Vungle.adPlayableEvent += adPlayableEvent;
	}
	
	public override void Init (params string[] key)
	{
		Debug.Log ("YXC" + "  Vungle Init android key  " + key [0] + "Vungle Init iOS key  " + key [1]);
		Vungle.init (key [0], key [1], "");
		Vungle.setSoundEnabled (false);
	}
	
	#region  展示banner 视屏 插屏
	
	public override void PlayBanner (bool show)
	{
		showBanner = show;
		if (show) {
			
		} else {
			
		}
		//      AdsYuMiBannerUnity.setAdsYuMiViewHidden (!show);
	}
	
	public override void ShowIncentVideo (Action ac = null)
	{
		if (ac != null) {
			finishtodo = ac;
		} else {
			finishtodo = null;
		}
		
		ShowRewardedInterstitial ();
	}
	
	public override void ShowInterVideo (Action ac = null)
	{
		if (ac != null) {
			finishtodo = ac;
		} else {
			finishtodo = null;
		}
		
		ShowInterstitial ();
	}
	
	public override void ShowInterstitial ()
	{
		Debug.Log ("YXC " + "展示Vungle插屏视频");
		Vungle.playAd ();
	}
	
	public override void ShowRewardedInterstitial ()
	{
		Debug.Log ("YXC " + "展示Vungle激励视频");
		Vungle.playAd (true, "user-tag");
	}
	
	#endregion
	
	public override void AdsCallback (string ev)
	{
		if (ev.Contains ("REWARDAPPROVEDINFO")) {
			
			// The format would be "REWARDAPPROVEDINFO|AMOUNT|CURRENCY" so "REWARDAPPROVEDINFO|10|Coins" for example
			
			// Split the string based on the delimeter
			string[] split = ev.Split ('|');
			
			// Pull out the currency amount
			double amount = double.Parse (split [1]);
			
			// Pull out the currency name
			string currencyName = split [2];
			
			// Do something with the values from above.  For example, grant the coins to the user.
			Debug.Log ("YXC " + "complete Vungle video");
		} else if (ev.Contains ("LOADEDREWARDED")) {
			// A rewarded video was successfully loaded.
			Debug.Log ("YXC " + "load Vungle success");
		} else if (ev.Contains ("LOADREWARDEDFAILED")) {
			// A rewarded video failed to load.
			Debug.Log ("YXC " + "load Vungle fail go to load again");
			GetIncentivizedVideo ();
		} else if (ev.Contains ("HIDDENREWARDED")) {
			// A rewarded video was closed.  Preload the next rewarded video.
			Debug.Log ("YXC " + "Vungle视频/插屏 播放完成，Vungle自动取下次视频/插屏");
			if (finishtodo != null)
				finishtodo ();
		}
	}
	
	#region 获取视频和插屏
	
	public override void GetIncentivizedVideo ()
	{
	}
	
	public override void GetInterstitial ()
	{
	}
	
	#endregion
	
	#region 判断视频和插屏是否加载好
	
	public override bool IsIncentivizedAvailable ()
	{
		
		if (Vungle.isAdvertAvailable ()) {
			Debug.Log ("YXC " + "Vungle 视频加载好啦");
			return true;
		} else {
			Debug.Log ("YXC " + "Vungle 视频没加载好");
			return false;
		}
	}
	
	public override bool IsInterstitialAvailable ()
	{
		if (Vungle.isAdvertAvailable ()) {
			Debug.Log ("YXC " + "Vungle inter video加载好啦");
			return true;
		} else {
			Debug.Log ("YXC " + "Vungle inter video视频没加载好");
			return false;
		}
	}
	
	#endregion
	
	void onAdStartedEvent ()
	{
		Debug.Log ("YXC " + "onAdStartedEvent");
	}
	
	
	void onAdFinishedEvent (AdFinishedEventArgs arg)
	{
		Debug.Log ("YXC " + "onAdFinishedEvent. watched: " + arg.TimeWatched + ", length: " + arg.TotalDuration + ", isCompletedView: " + arg.IsCompletedView);
		if (arg.IsCompletedView) {
			AdsCallback ("HIDDENREWARDED");
		}
	}
	
	
	void adPlayableEvent (bool playable)
	{
		Debug.Log ("YXC " + "adPlayableEvent: " + playable);
	}
}
